package com.macro.mall.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.macro.mall.dto.ZsqAddr;
import com.macro.mall.mapper.UmsMemberReceiveAddressMapper;
import com.macro.mall.model.UmsMemberReceiveAddress;
import com.macro.mall.model.UmsMemberReceiveAddressExample;
import com.macro.mall.service.AddressService;
import com.macro.mall.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址管理Service实现类
 * Created by macro on 2018/10/18.
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private UmsMemberReceiveAddressMapper companyAddressMapper;
    @Autowired
    private Utils utils;
    @Override
    public List<UmsMemberReceiveAddress> list() {

        return companyAddressMapper.selectByExample(new UmsMemberReceiveAddressExample());
    }

    @Override
    public int delect(Long id) {
       return companyAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(UmsMemberReceiveAddress omsCompanyAddress) {
        return companyAddressMapper.insert(omsCompanyAddress);
    }

    @Override
    public UmsMemberReceiveAddress abc(String addrName) {
            String strAddr = Utils.zsqAA(addrName);
            JSONObject jsonObject = JSON.parseObject(strAddr);
            JSONObject key = jsonObject.getJSONObject("key");
            UmsMemberReceiveAddress memberReceiveAddress = new UmsMemberReceiveAddress();
            if (null!=key) {
                memberReceiveAddress.setName(key.getString("name"));
                memberReceiveAddress.setPhoneNumber("mobile");
                memberReceiveAddress.setPostCode(key.getString("zipCode"));
                memberReceiveAddress.setProvince(key.getString("province"));
                memberReceiveAddress.setCity(key.getString("city"));
                memberReceiveAddress.setRegion(key.getString("area"));
                memberReceiveAddress.setDetailAddress(key.getString("detail"));
            }
            return memberReceiveAddress;
    }

    @Override
    public UmsMemberReceiveAddress selectAddr(Long id) {
        return companyAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateAddr(UmsMemberReceiveAddress umsMemberReceiveAddress) {
        return companyAddressMapper.updateByPrimaryKeySelective(umsMemberReceiveAddress);
    }
}
